package guggaTests.tasks
{
	import flexunit.framework.TestCase;
	
	import gugga.tasks.FictiveTask;
	import gugga.tasks.ITask;
	import gugga.tasks.events.ProgressiveTaskEvent;
	import gugga.tasks.events.TaskEvent;

	public class FictiveTaskTest extends TestCase
	{
		
		private var isFictiveTaskStarted:Boolean;
		private var isFictiveTaskProgress:Boolean;
		private var isFictiveTaskCompleted:Boolean;		
		private var isFictiveTaskInterrupted:Boolean;
		
		private var mProgressiveTaskEvent : ProgressiveTaskEvent;
		
		public function FictiveTaskTest(methodName:String=null)
		{
			super(methodName);
			
			isFictiveTaskStarted = false;
			isFictiveTaskProgress = false;
			isFictiveTaskCompleted = false;
			isFictiveTaskInterrupted = false;
		}

		public function testFictiveTaskCommonEvents() : void
		{	
			var fictiveTask:FictiveTask = new FictiveTask();

			fictiveTask.addEventListener(TaskEvent.START, onFictiveTaskStarted);
			fictiveTask.addEventListener(ProgressiveTaskEvent.PROGRESS, onFictiveTaskProgress);
			fictiveTask.addEventListener(TaskEvent.COMPLETED, onFictiveTaskCompleted);
			
			fictiveTask.start();
			
			assertTrue("FictiveTask should be started", isFictiveTaskStarted);
			assertTrue("FictiveTask should progress", isFictiveTaskProgress);
			assertProgressiveTaskEvent();
			assertTrue("FictiveTask should complete", isFictiveTaskCompleted);
			
			reset();
		}
		
		public function testRepeatableStart() : void
		{
			var fictiveTask : FictiveTask = new FictiveTask();
			
			fictiveTask.addEventListener(TaskEvent.START, onFictiveTaskStarted);
			fictiveTask.addEventListener(ProgressiveTaskEvent.PROGRESS, onFictiveTaskProgress);
			fictiveTask.addEventListener(TaskEvent.COMPLETED, onFictiveTaskCompleted);
					
			fictiveTask.start();
			
			assertTrue("FictiveTask should be started", isFictiveTaskStarted);
			assertTrue("FictiveTask should progress", isFictiveTaskProgress);
			assertProgressiveTaskEvent();
			assertTrue("FictiveTask should complete", isFictiveTaskCompleted);
			
			// try to start fictiveTask again:
			reset();
			fictiveTask.start();
			
			assertTrue("FictiveTask should be started for second time", isFictiveTaskStarted);
			assertTrue("FictiveTask should progress for second time", isFictiveTaskProgress);
			assertProgressiveTaskEvent();
			assertTrue("FictiveTask should complete for second time", isFictiveTaskCompleted);
			
			reset();
		}
		
		public function testIsImmediatelyInterruptable() : void
		{
			var fictiveTask:ITask = new FictiveTask();
			
			assertTrue("fictiveTask should be immediately interruptable", fictiveTask.isImmediatelyInterruptable());
		}
		
		public function testInterrupt() : void
		{	
			var fictiveTask : FictiveTask = new FictiveTask();
			
			fictiveTask.addEventListener(TaskEvent.START, onFictiveTaskStarted);			
			fictiveTask.addEventListener(TaskEvent.INTERRUPTED, onFictiveTaskInterrupted);
			
			fictiveTask.start();
			fictiveTask.interrupt();
			
			assertTrue("FictiveTask should be started", isFictiveTaskStarted);
			assertTrue("FictiveTask should be interrupted", isFictiveTaskInterrupted);
		}
		
		private function onFictiveTaskStarted(aEvent : TaskEvent) : void
		{
			var fictiveTask : FictiveTask = FictiveTask(aEvent.target);
			isFictiveTaskStarted = fictiveTask.isRunning();
		}
		
		private function onFictiveTaskProgress(aEvent : ProgressiveTaskEvent) : void
		{
			isFictiveTaskProgress = true;
			mProgressiveTaskEvent = aEvent;
		}
		
		private function onFictiveTaskCompleted(aEvent : TaskEvent) : void
		{
			isFictiveTaskCompleted = true;
		}
		
		private function onFictiveTaskInterrupted(aEvent : TaskEvent) : void
		{
			isFictiveTaskInterrupted = true;
		}
		
		private function assertProgressiveTaskEvent() : void
		{
			
			assertEquals(
					"FictiveTask progress percents should be 100", 
					mProgressiveTaskEvent.Percents, 
					100);
			assertEquals(
					"FictiveTask progress total should be 100", 
					mProgressiveTaskEvent.Total, 
					100);
			assertEquals(
					"FictiveTask progress current should be 100", 
					mProgressiveTaskEvent.Current, 
					100);
		}
		
		private function reset() : void
		{
			isFictiveTaskStarted = false;
			isFictiveTaskProgress = false;
			isFictiveTaskCompleted = false;
			isFictiveTaskInterrupted = false;
			
			mProgressiveTaskEvent = null;
		}
		
	}
}